package github.leavesczy.reactivehttpsamples.widget.dialog;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import github.leavesczy.reactivehttpsamples.R;
import github.leavesczy.reactivehttpsamples.util.Logger;
import github.leavesczy.reactivehttpsamples.widget.view.DYLoadingView;


public class LoadDialog extends AlertDialog {

    /**
     * canNotCancel, the mDialogTextView dimiss or undimiss flag
     */
    private boolean canNotCancel;
    /**
     * if the mDialogTextView don't dimiss, what is the tips.
     */
    private String tipMsg;

    private TextView mShowMessage;
    //    private final LoadingImageView loading_image;
    private DYLoadingView mDyLoading;
    private Context ctx;

    /**
     * the LoadDialog constructor
     *
     * @param ctx          Context
     * @param canNotCancel boolean
     * @param tipMsg       String
     */
    public LoadDialog(final Context ctx, boolean canNotCancel, String tipMsg) {
        super(ctx, R.style.loading_dialog);

        this.ctx = ctx;
        this.canNotCancel = canNotCancel;
        this.tipMsg = tipMsg;
    }

    public LoadDialog(final Context ctx) {
        super(ctx, R.style.loading_dialog);

        this.ctx = ctx;
        this.canNotCancel = false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_dialog_loading);
        Logger.d("<LoadDialog>onCreate>"+hashCode());
        mShowMessage = (TextView) findViewById(R.id.show_message);
        if (!TextUtils.isEmpty(this.tipMsg)) {
            mShowMessage.setText(this.tipMsg);
            mShowMessage.setVisibility(View.VISIBLE);
        } else {
            mShowMessage.setVisibility(View.GONE);
        }
        Window window = getWindow();
        window.setBackgroundDrawableResource(android.R.color.transparent);
        WindowManager.LayoutParams attributesParams = window.getAttributes();
//        attributesParams.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        attributesParams.dimAmount = 0.0f;
//        attributesParams.windowAnimations=R.style.DialogStyle;
        window.setAttributes(attributesParams);
//        window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//        loading_image = (LoadingImageView) findViewById(R.id.loading_image);
        mDyLoading = findViewById(R.id.dy_loading);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (canNotCancel) {
//                NToast.shortToast(getContext(), tipMsg);
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void show() {
        Logger.d("<LoadDialog>show>"+hashCode());
        super.show();
//        if (loading_image != null) {
//            loading_image.startAnim();
//        }
        if (mDyLoading != null) {
            mDyLoading.start();
        }
    }

    @Override
    public void dismiss() {
//        if (loading_image != null) {
//            loading_image.stopAnim();
//        }
        Logger.d("<LoadDialog>dismiss>"+hashCode());
        if (mDyLoading != null) {
            mDyLoading.stop();
        }
        super.dismiss();
        if(delayListener!=null){
            delayListener.dismiss();
        }

    }

    public void setDelayDismiss(String tipMsg,OnDismissResult delayListener){
        this.delayListener=delayListener;
       if(mShowMessage!=null){
           mShowMessage.setText(tipMsg);
           mShowMessage.postDelayed(new Runnable() {
               @Override
               public void run() {
                   dismiss();
               }
           },200);
       }else {
           dismiss();
       }
    }
   private OnDismissResult delayListener;
    public interface OnDismissResult{
        void dismiss();
    }
}
